package edu.buffalo.cse.cse486_586.simpledynamo;

import android.util.Log;
import edu.buffalo.cse.cse486_586.simpledynamo.util.AppUtil;

public class ControllerThread implements Runnable {
	private MainActivity activity;
	public ControllerThread(MainActivity ac) {
		this.activity = ac;
	}

	private String pkg = "";
	public synchronized void setPkg(String p) {
		this.pkg = p;
	}

	@Override
	public void run() {
		if (pkg.length() > 0) {
			String tokens[] = pkg.split(AppUtil.PACKAGE_DELIMITER);
			if (tokens.length > 0) {
				int opt = Integer.parseInt(tokens[0]);
				switch (opt) {
					case AppUtil.PACKAGE_INSERT :
						if (tokens.length == 4) {
							activity.commit(tokens[1], tokens[2], tokens[3]);
							activity.sendCommitPkg(tokens[1], tokens[2],
									tokens[3]);
						}
						break;
					case AppUtil.PACKAGE_COMMIT :
						if (tokens.length == 4) {
							activity.commit(tokens[1], tokens[2], tokens[3]);
						}
						break;
					case AppUtil.PACKAGE_GET :
						if (tokens.length == 3) {
							String displayStr = activity.lookUp(tokens[2]);
							activity.sendGetResponsePkg(tokens[1], displayStr);
						}
						break;
					case AppUtil.PACKAGE_GET_RESPONSE :
						String str = tokens[1];
						Log.d("retrieved content =====>", str);
						// send response to activity
						activity.addToListView(str);
						break;
					case AppUtil.PACKAGE_FAILUR_REPORT :
						activity.findFailur(tokens[1]);
					default :
						break;
				}

			}
		}
	}

}
